package com.github.appintro.internal;

import com.github.appintro.AppIntroBase;
import com.github.appintro.AppIntroViewPagerListener;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

public class AppIntroViewPager extends PageSlider implements Component.TouchEventListener {
    public boolean isFullPagingEnabled() {
        return isFullPagingEnabled;
    }

    public void setFullPagingEnabled(boolean fullPagingEnabled) {
        isFullPagingEnabled = fullPagingEnabled;
    }

    public boolean isPermissionSlide() {
        return isPermissionSlide;
    }

    public void setPermissionSlide(boolean permissionSlide) {
        isPermissionSlide = permissionSlide;
    }

    public AppIntroViewPagerListener getOnNextPageRequestedListener() {
        return onNextPageRequestedListener;
    }

    public void setOnNextPageRequestedListener(AppIntroViewPagerListener onNextPageRequestedListener) {
        this.onNextPageRequestedListener = onNextPageRequestedListener;
    }

    boolean isFullPagingEnabled = true;
    boolean isPermissionSlide = false;
    AppIntroViewPagerListener onNextPageRequestedListener;
    private float currentTouchDownX;
    private float currentTouchDownY;
    private long illegallyRequestedNextPageLastCalled = 0;
    private ScrollerCustomDuration customScroller;
    private AppIntroBase.OnPageChangeListener pageChangeListener;
    private Context context;

    public AppIntroViewPager(Context context, AttrSet attrs) {
        super(context, attrs);
        this.context = context;
        isFullPagingEnabled = true;
        setTouchEventListener(this);
    }

    public void addOnPageChangeListener(AppIntroBase.OnPageChangeListener listener) {
        super.addPageChangedListener(listener);
        this.pageChangeListener = (AppIntroBase.OnPageChangeListener) listener;
    }

    public void goToNextSlide() {
        int current = getCurrentPage();
        if (!LayoutUtil.isRtl(context)) {
            setCurrentItem(current + 1);
        } else {
            setCurrentItem(current - 1);
        }
    }

    public void goToPreviousSlide() {
        int current = getCurrentPage();
        if (!LayoutUtil.isRtl(context)) {
            setCurrentItem(current - 1);
        } else {
            setCurrentItem(current + 1);
        }
    }

    public void reCenterCurrentSlide() {
        int item = this.getCurrentPage();
        this.setCurrentItem(item);
    }

    public final boolean isFirstSlide(int size) {
        int item = this.getCurrentPage();
        if (LayoutUtil.isRtl(context)) {
            return item - size + 1 == 0;
        } else {
            return item == 0;
        }
    }

    public final boolean isLastSlide(int size) {
        int item = this.getCurrentPage();
        if (LayoutUtil.isRtl(context)) {
            return item == 0;
        } else {
            return item - size + 1 == 0;
        }
    }

    public final int getCurrentSlideNumber(int size) {
        int item = this.getCurrentPage();
        if (LayoutUtil.isRtl(context)) {
            return size - item;
        } else {
            return item + 1;
        }
    }

    public void setCurrentItem(int currentItem) {
        int oldItem = super.getCurrentPage();
        super.setCurrentPage(currentItem);
        if (oldItem == 0 && currentItem == 0) {
            if (pageChangeListener != null) {
                pageChangeListener.onPageChosen(0);
            }
        }
    }

    public void setScrollDurationFactor(double factor) {
        if (customScroller != null) {
            customScroller.setScrollDurationFactor(factor);
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (!handleTouchEvent(touchEvent)) {
            return false;
        }
        return false;
    }

    private boolean handleTouchEvent(TouchEvent event) {
        if (!this.isFullPagingEnabled) {
            return false;
        } else {
            switch (event.getAction()) {
                case 0:
                    this.currentTouchDownX = event.getPointerPosition(0).getX();
                    this.currentTouchDownY = event.getPointerPosition(0).getY();
                    break;
                default:
                    boolean canRequestNextPage = onNextPageRequestedListener != null ? onNextPageRequestedListener.onCanRequestNextPage() : true;
                    if (!canRequestNextPage && this.isSwipeForward(this.currentTouchDownX, event.getPointerPosition(0).getX())) {
                        if (this.userIllegallyRequestNextPage(event)) {
                            if (onNextPageRequestedListener != null) {
                                onNextPageRequestedListener.onIllegallyRequestedNextPage();
                            }
                        }
                        return false;
                    }
                    if (this.isPermissionSlide && this.isSwipeForward(this.currentTouchDownX, event.getPointerPosition(0).getX())) {
                        if (onNextPageRequestedListener != null) {
                            onNextPageRequestedListener.onUserRequestedPermissionsDialog();
                        }
                    }
            }
            return this.isFullPagingEnabled;
        }
    }

    private final boolean isSwipeForward(float oldX, float newX) {
        return LayoutUtil.isRtl(context) ? newX > oldX : oldX > newX;
    }

    private final boolean userIllegallyRequestNextPage(TouchEvent event) {
        if (System.currentTimeMillis() - this.illegallyRequestedNextPageLastCalled >= (long) 1000) {
            this.illegallyRequestedNextPageLastCalled = System.currentTimeMillis();
            return true;
        } else {
            return false;
        }
    }

}
